package spill;

import java.awt.Color;
import java.awt.Graphics;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Item implements Drawable {
	private static final Color pink = new Color(253, 113, 190);
	private static final Color brown = new Color(99, 68, 48);
	private static final Color skin = new Color(208, 177, 157);
	private static final Color orange = new Color(255, 128, 0);
	protected char[][] design;
	protected char[][] color;
	protected int x;
	protected int y;
	protected double charWidth = 6;
	protected double charHeight = 10;
	protected Engine engine;
	
	public Item(String colorFile, String designFile, Engine en) {
		engine = en;
		try {
			BufferedReader cReader = new BufferedReader(new FileReader(colorFile));
			BufferedReader dReader = new BufferedReader(new FileReader(designFile));
			int height = Integer.parseInt(dReader.readLine());
			design = new char[height][0];
			color = new char[height][0];
			String dLine;
			String cLine;
			for (int i = 0; i < height; i++) {
				dLine = dReader.readLine();
				cLine = cReader.readLine();
				if (dLine.length() > cLine.length()) {
					dLine = dLine.substring(0, cLine.length());
				}
				else {
					cLine = cLine.substring(0, dLine.length());
				}
				design[i] = dLine.toCharArray();
				color[i] = cLine.toCharArray();
			}
			cReader.close();
			dReader.close();
			
		} catch (FileNotFoundException e) {
			System.out.println("Kunne ikke finne fil, ");
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("Feil ved lesing av "+designFile);
		}
	}
	
	public Item(char[][] design, char[][] color, Engine e) {
		engine = e;
		this.color = color;
		this.design = design;
	}
	public void setX(int x) {
		this.x = x;
	}
	public int getX() {
		return x;
	}
	public void setY(int y) {
		this.y = y;
	}
	public int getY() {
		return y;
	}
	public void setCoord(int x, int y) {
		this.x = x;
		this.y = y;
	}
	public char[][] getDesign() {
		return design;
	}
	public char[][] getColor() {
		return color;
	}
	protected static Color getColor(char c) {
		if (c == 'p') {
			return pink;
		}
		else if (c == 'b') {
			return Color.blue;
		}
		else if (c == 'y') {
			return Color.yellow;
		}
		else if (c == 'r') {
			return Color.red;
		}
		else if (c == 'g') {
			return Color.green;
		} else if (c == 'o') {
			return orange;
		} else if (c == 's') {//skin color
			return skin;
		} else if (c == 'w') {//brown
			return brown;
		} else {
			return Color.black;
		}
	}
	public void draw(Graphics g) {
		char lastColor = color[0][0];
		int startJ = 0;
		g.setColor(getColor(color[0][0]));
		for (int i = 0; i < design.length; i++) {
			startJ = 0;
			for (int j = 0; j < design[i].length; j++) {
				if (color[i][j] != 'a' && color[i][j] != lastColor) {
					g.drawChars(design[i], startJ, j-startJ, (int)(x+charWidth*startJ), (int)(y+charHeight*i));
					startJ = j;
					lastColor = color[i][j];
					g.setColor(getColor(lastColor));
					
				}
			}
			g.drawChars(design[i], startJ, design[i].length-startJ, (int)(x+startJ*charWidth), (int)(y+charHeight*i));
		}
	}
}